package bancodedados;

import java.util.ArrayList;
import java.util.List;

import estruturas.Aluno;
import estruturas.Disciplina;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DisciplinaDAO {
	    private SQLiteDatabase db;
		private Helper helper;
		private String[] columns = {"id", "nome"};
		private long insertNome;
		
		public DisciplinaDAO(Context context)
		{
			helper = new Helper(context);
		}
		
		public void open() throws SQLException
		{
			db = helper.getWritableDatabase();
		}
		
		public void close()
		{
			db.close();
		}

		public Disciplina addDisciplina(Disciplina d)
		{
			ContentValues values = new ContentValues();
			values.put("nome", d.getNome());
			
			setInsertNome(db.insert("Disciplina", null, values));
			Cursor cursor = db.query("Disciplina", columns, null, null, null, null, null,null);
									
			
			cursor.moveToFirst();
			Disciplina newDisciplina = cursorToDisciplina(cursor);
			cursor.close();
			
			return newDisciplina;
		}
		
			
		

		public List<String> getLista()
		{
			List<String> disciplina = new ArrayList<String>();
			
			Cursor cursor = db.query("Disciplina", columns, null, null, null, null, null);
			
			cursor.moveToFirst();
			while(!cursor.isAfterLast())
			{
				disciplina.add(cursorToDisciplina(cursor).getNome());
				cursor.moveToNext();
			}
			
			cursor.close();
			return disciplina;
		}
		
		public boolean existeDisciplina(String n){
			Cursor cursor = db.query("Disciplina", columns, "Lower(nome) = '" + n.toLowerCase() + "'", null, null, null, null);
			
			if (cursor.getCount()>0){
				cursor.close();
				return true;
			}else{
				cursor.close();
				return false;
			}
			
		}
		
		
		private Disciplina cursorToDisciplina(Cursor cursor)
		{
			Disciplina disciplina = new Disciplina();
			
			disciplina.setNumero(cursor.getInt(0));
			disciplina.setNome(cursor.getString(1));
			return disciplina;
		}

		public long getInsertNome() {
			return insertNome;
		}

		public void setInsertNome(long insertNome) {
			this.insertNome = insertNome;
		}
	}


